package com.dawnClass.controller;

import com.dawnClass.service.CodeService;
import com.dawnClass.util.ServiceResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
@Controller
public class CodeController {
    @Autowired
    private CodeService codeService;


    @PostMapping("code")
    public ModelAndView getByEmail(@RequestParam String consumerEmail,HttpSession session){
        System.out.println("2222222222222222222222222");
        System.out.println("session id"+session.getId());
        ServiceResponse serviceResponse = codeService.getByEmail(consumerEmail);
        ModelAndView mav = new ModelAndView();
        System.out.println(serviceResponse);
        if(serviceResponse.getCode() == 200){ // 用户找到了，也就是邮箱是正确的，跳转到首页index.jsp , 首页中显示用户名
            session.setAttribute("ServiceResponse",serviceResponse); // 绑定登录用户 到session中

        }else{ //  用户名和密码是错误的，跳转到login.jsp ，提示 用户名或密码错误，请重新输入
            mav.addObject("loginMsg3","邮箱错误，请重新输入");
            mav.setViewName("login/code");
        }
        return mav;
    }
    @PostMapping("Validation")
    public ModelAndView getByEmailAndPassword(String consumerEmail,Integer password,HttpSession session){
        System.out.println("1111111111111111111111111111111");
        System.out.println("session id"+session.getId());
        ServiceResponse serviceResponse = codeService.getByEmailAndPassword(consumerEmail,password);

        ModelAndView mav = new ModelAndView();
        System.out.println(serviceResponse);
        if(serviceResponse.getCode() == 200){ // 用户找到了，也就是邮箱是正确的，跳转到首页index.jsp , 首页中显示用户名
             session.setAttribute("ServiceResponse",serviceResponse); // 绑定登录用户 到session中
             mav.setViewName("index");
        }else{ //  用户名和密码是错误的，跳转到login.jsp ，提示 用户名或密码错误，请重新输入
            mav.addObject("loginMsg3","验证码错误，请重新输入");
            mav.setViewName("login/code");
        }
        return mav;
    }


}
